package com.atguigu.java;

import org.junit.Test;

import java.io.*;

public class BufferedTest {
    public void copy(String srcPath,String destPath) throws IOException {
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
             bis = new BufferedInputStream(new FileInputStream(new File(srcPath)));
             bos = new BufferedOutputStream(new FileOutputStream(new File(destPath)));
            byte[] bytes = new byte[1024];
            int len;
            while((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bos!=null)
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if(bis!=null)
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }
    @Test
    public void copyTest() throws IOException {
        long start = System.currentTimeMillis();
        String srcPath="E:\\test\\1.txt";
        String destPath="E:\\test\\2.txt";
        copy(srcPath,destPath);
        long end = System.currentTimeMillis();
        System.out.println("复制花费时间为"+(end-start));


    }
    @Test
    public void BufferTest() throws IOException {
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
             bis = new BufferedInputStream(new FileInputStream(new File("1.jpg")));
             bos = new BufferedOutputStream(new FileOutputStream(new File("3.jpg")));
            byte[] bytes = new byte[5];
            int len;
            while((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bos!=null)
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if(bis!=null)
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }


        }

    }
    @Test
    public void test4() throws IOException {
        BufferedReader br=null;
        BufferedWriter bw=null;
        try {
             br = new BufferedReader(new FileReader(new File("hello.txt")));
             bw = new BufferedWriter(new FileWriter(new File("hello4.txt")));
//            char[] chars = new char[1024];
//            int len;
//            while((len=br.read(chars))!=-1){
//                bw.write(chars,0,len);
//            }
            String data;
            while((data=br.readLine())!=null){
//                bw.write(data+"\n");
                bw.write(data);
                bw.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bw!=null)
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if(br!=null)
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }
}
